package com.senlingspace.web.controller.headquarters;

import java.util.List;

import com.senlingspace.system.domain.dto.ShopProjectDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.senlingspace.common.annotation.Log;
import com.senlingspace.common.core.controller.BaseController;
import com.senlingspace.common.core.domain.AjaxResult;
import com.senlingspace.common.enums.BusinessType;
import com.senlingspace.system.domain.Project;
import com.senlingspace.system.service.IProjectService;
import com.senlingspace.common.core.page.TableDataInfo;

/**
 * 项目管理Controller
 * <p>
 * 总部
 *
 * @author SenlingSpace
 * @date 2024-06-26
 */
@RestController
@RequestMapping(path = "/system/project")
public class SlkjProjectController extends BaseController {

    @Autowired
    private IProjectService projectService;

    /**
     * 查询项目列表
     */
    @PreAuthorize("@ss.hasPermi('system:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(Project project) {
        List<Project> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    /**
     * 获取项目详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:project:query')")
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") Long projectId) {
        return success(projectService.selectProjectByProjectId(projectId));
    }

    /**
     * 新增项目
     */
    @PreAuthorize("@ss.hasPermi('system:project:add')")
    @Log(title = "项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Project project) {
        project.setCreateBy(super.getUserId() + "");
        return toAjax(projectService.insertProject(project));
    }

    /**
     * 修改项目
     */
    @PreAuthorize("@ss.hasPermi('system:project:edit')")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Project project) {
        project.setUpdateBy(super.getUserId() + "");
        return toAjax(projectService.updateProject(project));
    }

    /**
     * 删除项目
     */
    @PreAuthorize("@ss.hasPermi('system:project:remove')")
    @Log(title = "项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable Long[] projectIds) {
        int i = projectService.deleteProjectByProjectIds(projectIds);
        AjaxResult json = new AjaxResult();
        if( i == 0){
            return  AjaxResult.error("删除失败，项目还在售卖，请价检查设置");
        }else {
            return  toAjax(i);
        }
    }

    /**
     * 获取项目，下拉框列表
     *
     * @param projectName
     * @return
     */
    @GetMapping("/projectSelected")
    public AjaxResult projectSelected(@RequestParam(required = false) String projectName) {
        return projectService.projectSelected(projectName);
    }

    /**
     * 项目详情
     *
     * @param id
     * @return {@link AjaxResult }
     */
    @GetMapping("/detail")
    public AjaxResult detail(@RequestParam String id) {
        return projectService.projectDetail(id);
    }

}